package com.csf.struct.composite;

import java.util.Enumeration;

/**
 * 组合模式：将多个对象组合在一起进行操作，常用于表示树形结构中，例如二叉树，树等
 * Date: 2018/9/6
 * Time: 17:19
 *
 * @author chenshf
 */
public class Test {

    public static void main(String[] args) {

        Tree tree = new Tree("A");

        TreeNode nodeB = new TreeNode("B");

        TreeNode nodeC = new TreeNode("C");

        TreeNode nodeD = new TreeNode("D");

        nodeB.addChild(nodeC);
        nodeB.addChild(nodeD);

        tree.root.addChild(nodeB);

        getChild(tree.root);

        System.out.println(nodeD.getParent().getParent().getName());

    }

    /**
     * 递归子节点
     * @param parents
     */
    public static void getChild(TreeNode parents){
        Enumeration<TreeNode> childs = parents.getChilds();
        while (childs.hasMoreElements()){
            TreeNode nextElement = childs.nextElement();
            System.out.println(nextElement.getName());
            getChild(nextElement);
        }
    }
}
